/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package itproject.clients.gui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

/**
 *
 * @author tim
 */
public class EBInterfaceConfig {

    // for testing
    public static void main(String args[]) {
        new EBInterfaceConfig();
    }
    JFrame clientConfigFrame;
    private JMenuBar configJMenuBar;
    private JMenu fileJMenu;
    private JMenuItem exit;
    private JPanel contentPanel;
    private JPanel configPanel;
    private JLabel lblServerHost;
    private JTextField txfServerHost;
    private JButton btnOk, btnCancel;
    private JPanel buttonPanel;
    private EBInterface ei;

    public EBInterfaceConfig() {
        this.createAndShowGUI();
    }
     public EBInterfaceConfig(EBInterface ei) {
         this.ei = ei;
         this.createAndShowGUI();
    }

    public void createAndShowGUI() {
        // Set LAF to System Standard LAF
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception exc) {
            System.err.println("Error loading L&F: " + exc);
        }

        clientConfigFrame = new JFrame("EasyBooking Client Configuration");
        clientConfigFrame.setDefaultLookAndFeelDecorated(true);
        clientConfigFrame.setSize(500, 400);
        clientConfigFrame.setJMenuBar(this.createJMenuBar());
        clientConfigFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        clientConfigFrame.setContentPane(this.createContentPanel());
        clientConfigFrame.setResizable(false);
        clientConfigFrame.pack();
        clientConfigFrame.setVisible(true);


    }

    // Creates the Menu Bar
    public JMenuBar createJMenuBar() {

        configJMenuBar = new JMenuBar();
        fileJMenu = new JMenu("File");
        exit = new JMenuItem("Exit");

        exit.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        fileJMenu.add(exit);
        configJMenuBar.add(fileJMenu);
        return configJMenuBar;
    }

    public JPanel createContentPanel() {
        contentPanel = new JPanel();

        // Sets the Layout of the contentPanel
        contentPanel.setLayout(new GridBagLayout());
        GridBagLayout gbl = new GridBagLayout();

        // Places the GUI elements in the layout using addComponent                                                         x, y, w, h, wx, wy
        addComponent(contentPanel, gbl, this.createConfigPanel(), GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 0, 0, 1, 1, 0, 0);
        addComponent(contentPanel, gbl, this.createButtonPanel(), GridBagConstraints.NONE, GridBagConstraints.EAST, 0, 1, 1, 1, 0, 0);

        return contentPanel;
    }

    // Creates the ConfigPanel Form
    public JPanel createConfigPanel() {
        configPanel = new JPanel();

        // Sets the border
        configPanel.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createTitledBorder("Easybooking Client Configuration"),
                BorderFactory.createEmptyBorder(20, 20, 20, 20)));

        // Sets the layout of the configPanel
        configPanel.setLayout(new GridBagLayout());
        GridBagLayout gbl = new GridBagLayout();

        // Places the GUI elements in the layout using addComponent                                             x, y, w, h, wx, wy
        lblServerHost = new JLabel("EasyBooking-Server:");
        addComponent(configPanel, gbl, lblServerHost, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 0, 0, 1, 1, 0, 0);

        txfServerHost = new JTextField(20);
        addComponent(configPanel, gbl, txfServerHost, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 1, 0, 1, 1, 0, 0);


        return configPanel;

    }

    // Creates the button panel
    public JPanel createButtonPanel() {
        buttonPanel = new JPanel();
        btnOk = new JButton("OK");
        btnCancel = new JButton("Cancel");

        // Sets the buttons the same size
        btnOk.setPreferredSize(btnCancel.getPreferredSize());

        // Registrate action listeners
        btnCancel.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        
        // Adds the buttons to the panel
        buttonPanel.add(btnOk);

        //
        btnOk.setActionCommand("connectToServer");
        btnOk.addActionListener(ei);
        
        buttonPanel.add(btnCancel);
        return buttonPanel;
    }

    // Adds the elements to the layout
    static void addComponent(Container cont, GridBagLayout gbl, Component c, int fill, int anchor, int x, int y, int width, int hight, double weightx, double weighty) {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = fill;
        gbc.anchor = anchor;
        gbc.gridx = x;
        gbc.gridy = y;
        gbc.gridwidth = width;
        gbc.gridheight = hight;
        gbc.weightx = weightx;
        gbc.weighty = weighty;
        gbc.insets = new Insets(10, 10, 10, 10);
        //gbl.setConstraints(c, gbc);
        cont.add(c, gbc);

    }
}
